//
//  WGGeneratePoster.m
//  iOSComponentProjrect
//
//  Created by 帅棋 on 2020/12/18.
//  Copyright © 2020 WG. All rights reserved.
//

#import "WGGeneratePoster.h"
#import "Config.h"

@interface WGGeneratePoster ()

@end

@implementation WGGeneratePoster

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //一般的view生成图片方法
    UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame) - 0 * 2, CGRectGetHeight(self.view.frame) - 0 * 2)];
    [self.view addSubview:subview];
//    [self onUIImageSubViewWithView:subview];
//
//    UIGraphicsBeginImageContext(subview.bounds.size);
//    self.view.backgroundColor = [UIColor whiteColor];
//    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
//
    UIImageView *ig=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(subview.frame), CGRectGetHeight(subview.frame) )];
    ig.backgroundColor = [UIColor yellowColor];
    ig.image = [self onUIImageSubViewWithView:subview];
    [subview addSubview:ig];
}

//view 需要生成的图片
- (UIImage *)onUIImageSubViewWithView:(UIView *)view
{
    //view容器
    UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.frame), CGRectGetWidth(view.frame) * 5.5 /4)];
    subview.backgroundColor = [UIColor greenColor];
    [view addSubview:subview];
    
    //主图
    UIImageView *mainimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(subview.frame), CGRectGetWidth(subview.frame))];
    mainimgv.backgroundColor = [UIColor purpleColor];
    [subview addSubview:mainimgv];
    mainimgv.image = [UIImage imageNamed:@"1.png"];
    
    //小图标
    UIImageView *xiaoimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.frame) / 20, CGRectGetWidth(view.frame) / 20)];
    xiaoimgv.backgroundColor = [UIColor purpleColor];
    [subview addSubview:xiaoimgv];
    [xiaoimgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainimgv.mas_bottom).mas_offset(10);
        make.left.equalTo(subview).mas_offset(10);
        make.size.mas_offset(CGSizeMake(CGRectGetWidth(view.frame) / 20, CGRectGetWidth(view.frame) / 20));
        
    }];
    
    //简介
    UILabel *nametxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    nametxt.text = @"      阿斯顿发大发大发是的发送到发送到发送到附近阿萨德放假啊收到了飞机阿斯顿发卡就是大幅拉升的减肥啦圣诞节费拉达斯放假啊了";
    nametxt.numberOfLines = 3;
    [subview addSubview:nametxt];
    [nametxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainimgv.mas_bottom).mas_offset(10);
        make.left.equalTo(subview).mas_offset(10);
        make.width.mas_offset(CGRectGetWidth(mainimgv.frame) / 2);
    }];
    
    //小itemview
    UIView *xiaosubview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 20)];
    xiaosubview.layer.masksToBounds = YES;
    [subview addSubview:xiaosubview];
    xiaosubview.layer.borderColor = [UIColor redColor].CGColor;
    xiaosubview.layer.borderWidth = 0.5;
    [xiaosubview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nametxt.mas_bottom).mas_offset(10);
        make.left.equalTo(subview).mas_offset(10);
        make.size.mas_offset(CGSizeMake(60, 20));
    }];
    xiaosubview.layer.cornerRadius = CGRectGetHeight(xiaosubview.frame) / 10;
    
    //xiao uilatxt
    UILabel *xiaouilatxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    xiaouilatxt.backgroundColor = [UIColor redColor];
    xiaouilatxt.textColor = [UIColor whiteColor];
    xiaouilatxt.font = [UIFont systemFontOfSize:11];
    [xiaosubview addSubview:xiaouilatxt];
    xiaouilatxt.text = @"券";
    xiaouilatxt.textAlignment = NSTextAlignmentCenter;
    [xiaouilatxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xiaosubview).mas_offset(0);
        make.left.equalTo(xiaosubview).mas_offset(0);
        make.bottom.equalTo(xiaosubview).mas_offset(0);
        make.width.mas_offset(CGRectGetWidth(xiaosubview.frame) / 3);
    }];
    
    //xiao 优惠券
    UILabel *xiaoquantxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    xiaoquantxt.text = @"¥50";
    xiaoquantxt.font = [UIFont systemFontOfSize:11];
    xiaoquantxt.textColor = [UIColor redColor];
    [xiaosubview addSubview:xiaoquantxt];
    xiaoquantxt.textAlignment = NSTextAlignmentCenter;
    [xiaoquantxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xiaosubview).mas_offset(0);
        make.left.equalTo(xiaouilatxt.mas_right).mas_offset(0);
        make.bottom.equalTo(xiaosubview).mas_offset(0);
        make.width.mas_offset(CGRectGetWidth(xiaosubview.frame) * 2 / 3);
    }];
    
    //优惠价
    UILabel *pricetxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    pricetxt.text = @"¥50.0";
    pricetxt.font = [UIFont systemFontOfSize:15];
    pricetxt.textColor = [UIColor redColor];
    [subview addSubview:pricetxt];
    [pricetxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xiaosubview.mas_bottom).mas_offset(10);
        make.left.equalTo(subview).mas_offset(10);
        make.bottom.equalTo(subview).mas_offset(-10);
    }];
    
    //原价
    UILabel *oldpricetxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    oldpricetxt.text = @"¥150.0";
    oldpricetxt.font = [UIFont systemFontOfSize:13];
    oldpricetxt.textColor = [UIColor grayColor];
    [subview addSubview:oldpricetxt];
    [oldpricetxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xiaosubview.mas_bottom).mas_offset(12);
        make.left.equalTo(pricetxt.mas_right).mas_offset(5);
        make.bottom.equalTo(subview).mas_offset(-10);
    }];
    
    
    //链接二维码
    CGFloat qrH = CGRectGetHeight(subview.frame) - CGRectGetHeight(mainimgv.frame) - 10 * 2;
    UIImageView *qrimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    qrimgv.backgroundColor = [UIColor yellowColor];
    [subview addSubview:qrimgv];
    [qrimgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainimgv.mas_bottom).mas_offset(10);
        make.right.equalTo(subview).mas_offset(-10);
        make.size.mas_offset(CGSizeMake(qrH, qrH));
    }];
    
    //生成图片
    return [self makeImageWithView:subview withSize:CGSizeMake(subview.frame.size.width, subview.frame.size.height)];
}

//这个方法生成的图片不太清晰  不过把这个方法修改一下  就可以了
#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}


@end
